"use client"

import { ColumnDef } from "@tanstack/react-table"
import { LockRecord } from "@/types/admin"
import { Badge } from "@/components/ui/badge"
import { ArrowUpDown } from "lucide-react"
import { Button } from "@/components/ui/button"

const resultStatusMap = {
  success: { label: "成功", color: "bg-green-500" },
  failure: { label: "失败", color: "bg-red-500" },
};

export const columns: ColumnDef<LockRecord>[] = [
  {
    accessorKey: "lockName",
    header: "锁名称",
  },
  {
    accessorKey: "keyName",
    header: "钥匙名称",
  },
  {
    accessorKey: "keyType",
    header: "钥匙类型",
    cell: ({ row }) => {
      const type = row.getValue("keyType") as 'bluetooth' | 'nfc';
      return <Badge variant="outline">{type === 'bluetooth' ? '蓝牙' : 'NFC'}</Badge>;
    },
  },
  {
    accessorKey: "taskName",
    header: "任务名称/备注",
  },
  {
    accessorKey: "department",
    header: "部门",
  },
  {
    accessorKey: "user",
    header: "用户",
  },
  {
    accessorKey: "type",
    header: "类型",
    cell: ({ row }) => {
      const type = row.getValue("type") as 'open' | 'close';
      return type === 'open' ? '开锁' : '关锁';
    },
  },
  {
    accessorKey: "time",
    header: ({ column }) => (
      <Button
        variant="ghost"
        onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
      >
        时间
        <ArrowUpDown className="ml-2 h-4 w-4" />
      </Button>
    ),
  },
  {
    accessorKey: "result",
    header: "结果",
    cell: ({ row }) => {
      const result = row.getValue("result") as keyof typeof resultStatusMap;
      const { label, color } = resultStatusMap[result] || { label: "未知", color: "bg-gray-500" };
      return <Badge className={`${color} text-white`}>{label}</Badge>;
    },
  },
  {
    accessorKey: "reason",
    header: "失败原因",
    cell: ({ row }) => row.getValue("reason") || "-",
  },
]; 